package com.example.springmvcdemo.controller;

import com.example.springmvcdemo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "/sayHi", method = RequestMethod.GET)
    // 默认情况下支持get和post请求 ,可以通过method = RequestMethod.POST设置请求方式
    public String sayHi(String name) {// 获取单个参数 参数名称前后端保持一致 前端没有传值的情况下的默认值是null 包装类的默认情况否是null
        return "UserController Hi springMVC" + name;
    }

    @PostMapping("hello")
    public String hello() {
        return "hello springMVC";
    }

    @GetMapping("hello2")
    public String hello2(User user) {
        return user.toString();
    }

    @RequestMapping("hello3")
    public Object hello3(@RequestBody User user) { // 前端发送的请求时jason对象的情况下，不能使用一般情况下的接收对象的代码，需要使用@RequsetBody注解
        return user;
    }

    @GetMapping("/rename")
    public String rename(@RequestParam("username") String name, String password) {// 将前端传过来的username重命名为name
        return name + "--" + password;
    }

    @RequestMapping("hello4")
    public User sayHi(@RequestBody User user) {
        return user;
    }

    //从基础的URL中获取参数
    @RequestMapping("/hello5/{name}/{password}")
    public Object hello4(@PathVariable("name") String username, @PathVariable String password) {
        return "name:" + username + "| password:" + password;
    }

    // 模拟接收前端上传的文件
    @RequestMapping("upload")
    public Object upload(@RequestPart("myimg") MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File saveFile = new File("F:\\Download\\" + fileName);
        try {
            file.transferTo(saveFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @RequestMapping("/getCookie")
    public String cookie(@CookieValue("name") String name) {
        return "获取Cookie --> name：" + name;
    }

    @RequestMapping("/setSess")
    public String setsess(HttpServletRequest request) {
        // 获取Session对象
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute("username", "guan");
        }
        return "session 存储成功";
    }

    @RequestMapping("/getSess")
    public String sess(HttpServletRequest request) {
        // 如果 session 不存在, 不会⾃动创建
        HttpSession session = request.getSession(false);
        String username = null;
        if (session != null && session.getAttribute("username") != null) {
            username = (String) session.getAttribute("username");
        }
        return "username：" + username;
    }

    @RequestMapping("/getSess2")
    @ResponseBody
    public String sess2(@SessionAttribute(value = "username", required = false) String username) {
        return "username：" + username;
    }

    @RequestMapping("/getHeader")
    public String param10(HttpServletRequest request, HttpServletResponse response) {
        String userAgent = request.getHeader("User-Agent");
        return "User-Agent ：" + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String header(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent：" + userAgent;
    }


    @RequestMapping("/index")
    public Object index() {
        return "/index.html";
    }


    @RequestMapping("/getJSON")
    @ResponseBody
    public HashMap<String, String> method_8() {
        HashMap<String, String> map = new HashMap<>();
        map.put("Java", "Java Value");
        map.put("MySQL", "MySQL Value");
        map.put("Redis", "Redis Value");
        return map;
    }

}
